<style>
.pure_table_extra { width: 750px; margin-top: 5px; }
.pure_table_extra th { height: 25px; line-height: 25px; cursor: pointer; }
.pure_table_extra img { cursor: pointer; margin-right: 2px; }
.pure_table_extra td { vertical-align: middle; }
</style>

<?php
$pg = Utils::get_param('pg', 1);
$data = $orm->get('pb_colg_unit', 't.no', 'ASC', $pg, SIZE, NULL);
?>

<button onclick="Url.redirect(__redmd, __sys + '.php?i=' + __id + '&__file=index_unit&pg=' + __data.p);" class="pure-button pure-button-primary">New Unit</button>
<table class="pure-table pure_table_extra" cellpadding=0 cellspacing=0>
  <thead>
    <tr>
      <th style="width: 40px;" class="ui-widget-header" onclick="return set_order('no');">No.</th>
      <th style="width: 40px;" class="ui-widget-header" onclick="return set_order('ver');">Ver</th>
      <th class="ui-widget-header" onclick="return set_order('name');">Name</th>
      <th style="width: 40px;" class="ui-widget-header" onclick="return set_order('lvl');">Lvl</th>
      <th style="width: 40px;" class="ui-widget-header" onclick="return set_order('credits');">Credits</th>
      <th style="width: 40px;" class="ui-widget-header">&nbsp;</th>
    </tr>
  </thead>
  <tbody id="data_body"></tbody>
</table>
<div>&nbsp;</div>
<div id="pagination"></div>

<script type="text/javascript">
  /*<![CDATA[*/
  var __data = JSON.parse('<?php echo addslashes(json_encode($data)); ?>');
  var __requrl = '<?php echo Utils::get_requrl($app_data); ?>';
  var __redmd = '<?php echo addslashes($app_data->sys->redmd); ?>';
  var __sys = '<?php echo addslashes($app_data->sys->php_name); ?>';
  var __id = '<?php echo $app_data->param_id; ?>';
  
  $(function() {
    render();
  });

  function render() {
    $('#pagination').pagination({
      items: __data.t,
      itemsOnPage: __data.s,
      currentPage: __data.p,
      cssStyle: 'compact-theme',
      onPageClick: function(pageNumber, event) {
        return Url.redirect(__redmd, __sys + '.php?i=' + '&pg=' + pageNumber);
      }
    });
    render_data();
  };
  
  function render_data() {
    Url.insert_param('o', __data.o);
    Url.insert_param('q', __data.q);
    
    $('#data_body').empty();
    for (var idx in __data.d) {
      var itm = __data.d[idx];
      $('#data_body').append(
        '<tr class="' + ((idx % 2 == 0) ? 'pure-table-even' : 'pure-table-odd') + '">' + 
          '<td>' + itm.no + '</td>' +
          '<td>' + itm.ver + '</td>' +
          '<td>' + itm.name + '</td>' +
          '<td>' + itm.lvl + '</td>' +
          '<td>' + itm.credits + '</td>' +
          '<td>' + 
            '<span style="cursor: pointer; float: left;" class="ui-button-icon-primary ui-icon ui-icon-pencil" onclick="Url.redirect(' + __redmd + ', \'' + __sys + '.php?i=' + __id + '&__file=index_unit&id=' + itm.id + '&pg=' + __data.p + '\');"></span>' +
            '<span style="cursor: pointer; float: left;" class="ui-button-icon-primary ui-icon ui-icon-closethick" onclick="delete_obj(' + itm.id + ');"></span>' +
          '</td>' + 
        '</tr>'
      );
    }
    if (__data.d.length == 0) {
      $('#data_body').append('<tr class="pure-table-even"><td colspan=' + $('#class_head tr').children('th').length + '>None</td></tr>');
    }
  };
  
  function delete_obj(id) {
    var req = Utils.get_by_id(id, __data.d);
    req.__pg = __data.p;
    
    Ajax.request(
      __requrl + 'delete_obj',
      'req=' + Utils.encodeURI(JSON.stringify(req)),
      function(rslt) {
        __data = rslt;
        render();
      },
      { confirm: 1 }
    );
  };

  function set_order(o) {
    var req = {
      q: (__data.q) == 'ASC' ? 'DESC' : 'ASC',
      o: o,
      pg: __data.p
    };
    Ajax.request(
      __requrl + 'get_objs',
      'req=' + Utils.encodeURI(JSON.stringify(req)),
      function(rslt) {
        __data = rslt;
        render();
      },
      null
    );
  };
  /*]]>*/
</script>